package ru.ifmo.ipimenov.ewf_svn.standalone.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.ItemProvider;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.UserProvider;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.db.Document;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.db.State;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.Item;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.Model;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.StateModel;
import ru.ifmo.ipimenov.ewf_svn.standalone.service.DocumentService;
import ru.ifmo.ipimenov.ewf_svn.standalone.service.impl.DocumentServiceImpl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * @author: Ilya Pimenov
 * Scartel Starlab company
 */
public class SetStateAction implements ActionListener {
    /**
     * Logger for all purposes
     */
    private static Log log = LogFactory.getLog(ProcessAction.class);

    private JList list;

    private JList list2;

    private Document d;

    private JFrame frame;

    public SetStateAction(JList list, Document d, JFrame frame, JList list2) {
        this.list = list;
        this.d = d;
        this.frame = frame;
        this.list2 = list2;
    }

    public void actionPerformed(ActionEvent e) {
        StateModel m = (StateModel) list.getModel();
        State state = m.getState(list.getSelectedIndex());
        DocumentService ds = new DocumentServiceImpl();
        ds.setState(d, state);
        frame.dispose();

        list2.setModel(new Model(retrieveData4List()));
    }

    private List<Item> retrieveData4List() {
        return ItemProvider.getInstance().loadItems(UserProvider.getInstance().getUser());
    }

}